import React, { useReducer, useContext } from 'react';

export default (reducer, actions, defaultValue) => {
  const Context = React.createContext();

  const Provider = ({ children }) => {
    const [state, dispatch] = useReducer(reducer, defaultValue);

    const boundActions = {};
    for (let key in actions) {
      boundActions[key] = actions[key](dispatch);
    }
    return <Context.Provider value={{ state, ...boundActions }}>{children}</Context.Provider>;
  };
  console.log('*******');

  return { Context, Provider };
};

// const useAuth = () => {
//   const auCt = useContext(Context);
//   console.log('1111111111111111');
//   if (!auCt) {
//     console.log('22222222222222');
//   }
//   const { state } = auCt;

//   return auCt;
// };
